package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemMessage;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.SystemMessageMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SystemMessageQueryObject;
import cn.wolfcode.trip.base.service.ISystemMessageService;
import cn.wolfcode.trip.base.util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SystemMessageServiceImpl implements ISystemMessageService {
    @Autowired
    private SystemMessageMapper systemMessageMapper;
    public PageInfo<?> query(SystemMessageQueryObject qo) {
        qo.setPageSize(3);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = systemMessageMapper.selectForList(qo);
        for (Map map : list) {
            Date createTime = (Date)map.get("createTime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("createTime",Object);
        }
        return new PageInfo<>(list);
    }

    public void insert(SystemMessage systemMessage) {
        systemMessageMapper.insert(systemMessage);
    }

    @Override
    public Integer countUnReadSystemMessages(SystemMessageQueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadSystemMessages = systemMessageMapper.countUnReadSystemMessages(qo);
        if(countUnReadSystemMessages == null){
            return 0;
        }else {
            return  countUnReadSystemMessages;
        }

    }

    @Override
    public void updateState(SystemMessage systemMessage) {
        systemMessageMapper.updateState(systemMessage);
    }
}
